package com.example.administrator.mobilesafe.receiver;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;

import com.example.administrator.mobilesafe.service.GPSService;
import com.example.administrator.mobliesafe.R;

public class SmsReceiver extends BroadcastReceiver {
    DevicePolicyManager devicePolicyManager;
    public SmsReceiver() {
    }
    private static final String TAG = "SMSReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {

        Object[] objs = (Object[]) intent.getExtras().get("pdus");
        for(Object obj: objs){
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
//			String sender = smsMessage.getOriginatingAddress();
            String body = smsMessage.getMessageBody();
            if("#*location*#".equals(body)){
                //获得手机位置地址
                Log.e(TAG, "获得手机位置地址");
                Intent gpsintent = new Intent(context,GPSService.class);
                context.startService(gpsintent);
                SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
                String sender = sp.getString("number","");
                String lastlocation = sp.getString("lastlocation", null);
                if(TextUtils.isEmpty(lastlocation)){
                    SmsManager.getDefault().sendTextMessage(sender, null, "location getting...", null, null);

                }else{
                    SmsManager.getDefault().sendTextMessage(sender, null, lastlocation, null, null);
                }
                abortBroadcast();
            }else if("#*alarm*#".equals(body)){
                //手机报警
                MediaPlayer mediaPlayer =MediaPlayer.create(context, R.raw.alarm);
                mediaPlayer.setVolume(1.0f, 1.0f);
                mediaPlayer.setLooping(false);
                mediaPlayer.start();
                abortBroadcast();
                Log.e(TAG, "手机报警");
                abortBroadcast();
            }else if ("#*lockscreen*#".equals(body)){
                //远程锁屏
        //        openAdmin(context);
                devicePolicyManager =(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
                devicePolicyManager.lockNow();
                Log.e(TAG, "远程锁屏");
                abortBroadcast();
            } else if ("#*wipedata*#".equals(body)) {
                devicePolicyManager =(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
                devicePolicyManager.wipeData(0);
                Log.e(TAG, "清楚数据");
                abortBroadcast();
            }

        }
    }

    private void openAdmin(Context context) {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        ComponentName componentName;
        componentName = new ComponentName(context,MyAdminniReceiver.class);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,componentName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"傻狗");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}
